package sk.naive.talker.server;

import sk.naive.talker.Board;
import sk.naive.talker.persistence.*;

import java.util.*;
import java.util.logging.Logger;
import java.rmi.RemoteException;

/**
 * Factory class for board object creation.
 * <p>
 * Used in board finder implementation (BoardRegistry).
 * Initialized when talker is started.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.10 $ $Date: 2005/02/16 19:27:21 $
 */
public class BoardFactory extends PersistentObjectFactory {
	private BoardRegistry finder;

	public BoardFactory(String propertiesBundleName) {
		super(propertiesBundleName);
	}

	public Board getBoard(String name) throws PersistenceException {
		return (Board) getPersistentObjectByName(name);
	}

	public Board createBoard(String name) throws PersistenceException, RemoteException {
		Board brd = (Board) create(name);
		brd.init();
		return brd;
	}

	protected PersistentObject persistentObjectInstance(Integer id) throws PersistenceException {
		return new Board(id, this);
	}

	protected int getType() {
		return DbLayer.OBJECT_TYPE_BOARD;
	}

	protected String getNamingProperty() {
		return Board.BPROP_NAME;
	}

	public BoardRegistry createBoardFinder(Set<String> boardNames, Logger logger) throws PersistenceException, RemoteException {
		finder = new BoardRegistry(this);
		for (Object boardName1 : boardNames) {
			String boardName = (String) boardName1;
			Board board = getBoard(boardName);
			finder.addBoard(board);
		}
		for (Object o : finder.boards()) {
			((Board) o).init();
		}
		return finder;
	}
}
